﻿using BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryMIS2019
{
    public partial class Login : Form
    {
        public string userName;//用户账号
        public string pwd;//用户密码
        string threshold = "";//控制阀值（焦点失去&获取）
        public Login()
        {
            InitializeComponent();
            if (this.tbUserName.Text != "输入账号")
            {
                userName = this.tbUserName.Text.Trim();
            }
            if (this.tbUserName.Text != "输入密码")
            {
                userName = this.tbPassword.Text.Trim();
            }

        }

        /// 清空当前窗体中的文本框内容
        private void ClearText()
        {
            foreach (Control Ctrol in this.Controls)
            {
                if (Ctrol is TextBox)
                {
                    Ctrol.Text = "";
                }
            }
        }
        //用户文本框事件处理
        private void UserNameEvent()
        {
            switch (threshold)
            {
                case "Enter":
                    if (this.tbUserName.Text == "输入账号")
                        this.tbUserName.Clear();//清空文本框
                    break;
                case "Leave":
                    if (this.tbUserName.TextLength == 0)
                        this.tbUserName.Text = "输入账号";
                    break;
            }
            if(this.tbUserName.TextLength != 0  || this.tbUserName.Text != "输入账号")
                userName = this.tbUserName.Text.Trim();
        }
        /// 账号文本框焦点获取事件
        private void tbUserName_Enter(object sender, EventArgs e)
        {
            threshold = "Enter";
            UserNameEvent();
        }
        private void tbUserName_Leave(object sender, EventArgs e)
        {
            threshold = "Leave";
            UserNameEvent();
        }
        private void tbUserName_Click(object sender, EventArgs e)
        {
            UserNameEvent();
        }
        //密码输入框事件处理
        private void PasswordEvent()
        {
            switch (threshold)
            {
                case "Enter":
                    if (this.tbPassword.Text == "输入密码")
                    {
                        this.tbPassword.PasswordChar = '*';
                        this.tbPassword.Clear();//清空文本框
                    }
                    break;
                case "Leave":
                    if (this.tbPassword.TextLength == 0)
                    {
                        this.tbPassword.PasswordChar = default(char);
                        this.tbPassword.Text = "输入密码";
                    }
                    break;
            }
            if (this.tbPassword.TextLength != 0 && this.tbPassword.Text != "输入密码")
            {
                this.tbPassword.PasswordChar = '*';
                pwd = this.tbPassword.Text.Trim();
            }
        }
        private void tbPassword_Click(object sender, EventArgs e)
        {
            PasswordEvent();
        }

        /// 密码输入框的焦点失去事件
        private void tbPassword_Leave(object sender, EventArgs e)
        {
            threshold = "Leave";
            PasswordEvent();

        }
        /// 取消按钮单击事件
        private void btnCancel_Click(object sender, EventArgs e)
        {
            ClearText();    //调用ClearText()方法清空文本框内容
            this.tbPassword.PasswordChar = default(char);
            this.tbUserName.Text = "输入账号";
            this.tbPassword.Text = "输入密码";
        }
        /// 密码文本框焦点获取事件
        private void tbPassword_Enter(object sender, EventArgs e)
        {
            threshold = "Enter";
            PasswordEvent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (userName.Length == 0)
            {
                MessageBox.Show("请输入用户名！", "系统提示");
                this.tbUserName.Focus();//焦点定位
                return;
            }
            if (pwd.Length == 0)
            {
                MessageBox.Show("请输入密码！", "系统提示");
                this.tbPassword.Focus();
                return;
            }
            if(userName.Length==0 && pwd.Length == 0)
            {
                MessageBox.Show("用户名或密码不能为空！", "系统提示");
                return;
            }
            BusinessLogic logic = new BusinessLogic();//实例化业务逻辑类对象logic
            if (logic.UserLogin(userName, pwd))
            {
                MessageBox.Show("恭喜你，你已经登录成功！", "系统提示");

            }
            else
            {
                MessageBox.Show("用户名或密码错误，请验证后重新登录！", "系统提示");
            }
            
        }
    }
}
